﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Compiled.Win8StartWPF
{
	/// <summary>
	/// Interaction logic for IconButton.xaml
	/// </summary>
	public partial class IconButton : UserControl
	{
		public IconButton()
		{
			InitializeComponent();
		}

		private void Control_MouseEnter(object sender, MouseEventArgs e)
		{
			Panel1.Background = SystemColors.HighlightBrush;
		}

		private void Control_MouseLeave(object sender, MouseEventArgs e)
		{
			Panel1.Background = SystemColors.WindowBrush;
		}

		private void Control_MouseDown(object sender, MouseButtonEventArgs e)
		{
			Panel1.Background = SystemColors.ActiveCaptionBrush;
			if (e.ChangedButton == MouseButton.Left)
				onClicked(this);
		}

		private void Control_MouseUp(object sender, MouseButtonEventArgs e)
		{
			Panel1.Background = SystemColors.HighlightBrush;
		}

		public void LoadImage(string Filename)
		{
			if (!System.IO.File.Exists(Filename))
				IconImage.Source = null;

			switch (System.IO.Path.GetExtension(Filename).ToLower())
			{
				case ".bmp":
				case ".ico":
				case ".gif":
				case ".jpg":
					BitmapImage icon = new BitmapImage();
					icon.BeginInit();
					icon.UriSource = new Uri(Filename);
					icon.EndInit();
					IconImage.Source = icon;
					break;
				case ".lnk":
					Icon = Properties.Resources.StartMenu.ToBitmap();
					break;
				default:
					break;
			}
		}

		public string Text
		{
			get { return ShortcutName.Content.ToString(); }
			set { ShortcutName.Content = value; }
		}

		public delegate void ItemClickedEvent(IconButton sender);
		public event ItemClickedEvent Clicked;
		protected virtual void onClicked(IconButton sender)
		{
			if (Clicked != null)
				Clicked(sender);
		}

		System.Drawing.Bitmap _icon;
		public System.Drawing.Bitmap Icon
		{
			get
			{
				return _icon;
			}
			set
			{
				_icon = value;
				if (value != null)
					this.IconImage.Source = _icon.ToImageSource();
			}
		}

		public string Data
		{
			get;
			set;
		}

	}
}
